package global

import (
	"database/sql"
	"os"
	"sync/atomic"
	"time"
)

type (
	Conn struct {
		Device          string `json:"device"`
		Jdbc            string `json:"jdbc"`
		MaxIdle         int
		MaxOpen         int
		IdleTime        time.Duration
		LeftTime        time.Duration
		Optimizer       bool
		JdbcExpandValue []string
		Mode            string
		SqlMode         bool
	}
	DBGetCharacterS struct {
		DB    map[string]*sql.DB
		Input StartPartConfigInputP
	}
	DbParameterOptimizerS struct {
		DB        map[string]*sql.DB
		ShardName string
		Con       Conn
		Input     StartPartConfigInputP
	}
	ModifyParameterResultS struct {
		Time          string `json:"timestamp"`
		Mode          string `json:"mode"`
		Object        string `json:"object"`
		Seq           string `json:"seq"`
		ShardName     string `json:"shardName"`
		ParameterName string `json:"parameter"`
		OldValue      string `json:"oldValue"`
		NewValue      string `json:"newValue"`
		Status        bool   `json:"status"`
		Con           Conn   `json:"con"`
	}
	ConnPool struct {
		Single          *sql.DB
		Conn            map[string]*sql.DB
		Max             map[string]int64
		ParameterModify []ModifyParameterResultS
	}
	TableSumInput struct {
		LogSeq             int64
		TableInfo          TableInfo
		Db                 map[string]*sql.DB
		SqlFilter          SqlFilter
		IndexColumn        []string
		PartitionNameGarth []string
		Input              StartPartConfigInputP
	}
	StartPartConfigInputP struct {
		Scn                 string
		SqlExecStopTime     time.Duration
		Esa                 EncryptionProperties
		WhereSql            string
		WhereAdd            string
		SqlMode             bool
		TablePx             string
		FixRows             int64
		LowerCaseTableNames bool
		SqlLogBin           bool //是否忽略binlog
		Partition           PartitionOptions
	}
	SelectColumnInput struct {
		Schema, Table     string
		LogSeq            int64
		OutputColumnType  string //输出index||all，默认为索引列,all表示查询所有列，index表示查询索引列
		SelectColumn      []string
		IndexColumn       []string
		IndexName         string
		TableColumn       []TableMeta
		Esa               EncryptionProperties
		RowsActiveMapList map[string]string //行数据差异映射处理
	}
	WhereGenerateInput struct {
		Schema, Table   string
		LogSeq          int64
		NullConstraint  string //是否允许为null
		IndexColumn     []string
		ColumnType      []string
		NullProperty    map[int]int
		SqlFilter       SqlFilter
		LastModifyPoint *int64 //记录上一次联合索引的变化的索引列序号，用于判断是否进行拆分
		TableColumn     []TableMeta
		NullMark        map[int]int   //索引列中存在null值的列的序号
		NullColumn      []string      //允许为null的列
		NullBool        *int32        //bool         //当前是否需要判断null值
		LeftDirection   *atomic.Value //[]map[string]string //包含columName、columnDataLeft、querySymbolLeft
		RightDirection  *atomic.Value //[]map[string]string //包含columName、columnDataRight、querySymbolRight
	}
	PassParameter struct {
		ColumnType []string //索引列的数据类型
		Ws, Wc     []string
	}
	DdlColumn struct {
		Name         string
		SMeta, DMeta TableMeta
		PartColumn   []string //目标端的partition列集合
		PriColumn    []string //目标端的primary key列集合
		Attributes   *MetaAttributesAb
	}
	GetTableMeta struct {
		Comment           map[string]TableRecordMeta //表的comment备注信息
		//Comment           any
		//Character           any
		//Collation           any
		ColumnMeta        map[string] /*source/dest*/ []TableMeta
		IndexMeta         map[string] /*source/dest*/ IndexMerge
		PartitionMeta     map[string] /*source/dest*/ []TablePartitionConfig
		AlterStatus       bool //判断是否为create,true为alter，false为create
		MetaAbnormal      *MetaModifyAttributesAb
		IndexAbnormal     *IndexModifyAttributesAb
		PartitionAbnormal *PartitionModifyAttributesAb
	}
	SrcDstViewObjectResult struct {
		Src ViewObjectResult
		Dst ViewObjectResult
	}
	SrcDstJobObjectResult struct {
		Src JobObjectResult
		Dst JobObjectResult
	}
	SrcDstSeqObjectResult struct {
		Src SeqObjectResult
		Dst SeqObjectResult
	}
	SrcDstTriggerObjectResult struct {
		Src TriggerObjectResult
		Dst TriggerObjectResult
	}
	SrcDstForeignObjectResult struct {
		Src ForeignObjectResult
		Dst ForeignObjectResult
	}
	SrcDstFuncObjectResult struct {
		Src FuncObjectResult
		Dst FuncObjectResult
	}
	SrcDstProcedureObjectResult struct {
		Src ProcedureObjectResult
		Dst ProcedureObjectResult
	}
	SrcDstTypeObjectResult struct {
		Src TypeObjectResult
		Dst TypeObjectResult
	}
	SrcDstPackageObjectResult struct {
		Src PackageObjectResult
		Dst PackageObjectResult
	}
	GetObjectMeta struct {
		AlterStatus     bool //判断是否为create,true为alter，false为create
		JobResult       SrcDstJobObjectResult
		ViewResult      SrcDstViewObjectResult
		SeqResult       SrcDstSeqObjectResult
		TriggerResult   SrcDstTriggerObjectResult
		ForeignResult   SrcDstForeignObjectResult
		FuncResult      SrcDstFuncObjectResult
		ProcedureResult SrcDstProcedureObjectResult
		TypeResult      SrcDstTypeObjectResult
		PackageResult   SrcDstPackageObjectResult
		ObjectFixSql    []string
	}
	GetColumnTypeInput struct {
		LogSeq        int64
		Schema, Table string
		ColumnFilter  []string
		DB            map[string]*sql.DB
		Plan          map[string][]string
		Input         StartPartConfigInputP
		TableInfo     TableInfo
		DdlColumn     DdlColumn
		TableMeta     GetTableMeta
		TableColumn   []TableMeta
	}
	LimitOffset struct {
		Pagination *int64
		Segment    int
		ModifyS    *int32
	}
	TableInfo struct {
		SchemaTableGather map[string]int //key为schema+table的集合
		TableType         string         //普通表还是partition表，"single"为单表，"partition"为分区表
		RecoverDbName     string         //是指恢复库的库名
		RecoverTableName  string         //是指恢复库的库名
		Schema            string
		Table             string
		ShardName         string
		ColumnMet         []TableMeta
		BackendTableName  string
		PartitionName     string   //partition Name
		ColumnType        []string //列数据类型
		LobSwitch         bool     //是不是lob表
		HiddenIndex       bool     //是否查询隐藏索引，目前只有cluster有
		RowsLimit         int      //限制查询的行数
	}
	SqlFilter struct {
		WhereSql string
		WhereAdd string
	}
	NullObject struct {
		NullBool       bool
		NullLeftSplit  bool //是否包含>= 或者<= null，如果为true，则表示包含，需要进行拆分
		NullRightSplit bool //是否包含>= 或者<= null，如果为true，则表示包含，需要进行拆分
	}
	Instance struct {
		Db map[string]*sql.DB
	}
	Position struct {
		Base            Instance
		SqlExecStopTime time.Duration
	}
	//StartPart StartPart
	StartPart struct {
		LogSeq           int64
		Error            string
		DB               map[string]*sql.DB
		OutputColumnType string
		TableInfo        TableInfo
		//SortingObject    string //一级分拣和二级分拣的表示
		Limit         LimitOffset
		NullObject    NullObject
		PassP         PassParameter
		GetColumnType GetColumnTypeInput
		SelectColumn  SelectColumnInput
		WhereGenerate WhereGenerateInput
		Input         StartPartConfigInputP
	}
	RowsDataInput struct {
		LogSeq       int64
		DB           map[string]*sql.DB
		Sql          string
		TableInfo    TableInfo
		SelectColumn SelectColumnInput
		Input        StartPartConfigInputP
	}
	ExecSqlResult struct {
		Timeout int64
		Result  any
		ExecSql string
	}
	InsertPrefix struct {
		InsertColumn     []string //insert插入列名
		PlaceholderValue []string //用于prepare 预解析sql的占位符
		TableInfo        TableInfo
		LobSwitch        bool
		Ignore           bool
	}
	DeletePrefix struct {
		TableInfo TableInfo
		LobSwitch bool
	}
	InsertValue struct {
		TableInfo          TableInfo
		Prefix             string
		RowData            [][]*string
		ColData            []TableMeta
		IgnoreColumnLength bool //是否忽略表的字段长度和行长度不匹配问题，true表示忽略
		Esa                EncryptionProperties
	}
	DeleteValue struct {
		TableInfo          TableInfo
		Prefix             string
		RowData            [][]*string
		IndexName          string
		IndexType          string
		ColData            []TableMeta
		IndexColumnSeq     map[string]string
		IgnoreColumnLength bool //是否忽略表的字段长度和行长度不匹配问题，true表示忽略
		Esa                EncryptionProperties
	}
	Insert struct {
		Prefix InsertPrefix
		Value  InsertValue
	}
	Delete struct {
		Prefix DeletePrefix
		Value  DeleteValue
	}
	PrefixInput struct {
		LogSeq int64
		Insert Insert
		Delete Delete
	}
	WriteOptimizer struct {
		FixSum    int64
		SqlMode   bool
		SqlLogBin bool
		Thread    *int64
		QueueData chan any
		AvgRowsSize  int64
		Active    string //执行方式，是写入文件还是执行到库中
		FilePoint *os.File
	}
	Statistics struct {
		AbTotal        *int64 //差异总数
		IntTotal       *int64 //数值类型总数
		StringTotal    *int64 //字符类型总数
		TimeTotal      *int64 //时间类型总数
		MissTotal      *int64 //数据缺失总数
		RedundantTotal *int64 //数据多余总数
	}
	RowsActiveInput struct {
		LogSeq          int64
		DB              map[string]*sql.DB
		SqlExecStopTime time.Duration
		TableInfo       TableInfo
		Optimizer       WriteOptimizer
		SqlPrefix       string
		ApplySqlGather  any
	}
	OptimizerOrderBy struct {
		SrcArgRowsLength map[string]any
		SrcShardName     map[string]string
		DestShardName    map[string]string
	}
	Alter struct {
		Active     string   //alter 执行的方式，有两种方式：table，file
		SqlGather  []string //alter sql语句集合
		FilePoint  *os.File
		FilePoint1 any
	}
	Create struct {
		Type    string //create的类型
		ExecSql string
	}
	Drop struct {
		Type    string //create的类型
		ExecSql []string
	}
	TableActiveInput struct {
		LogSeq           int64
		DB               map[string]*sql.DB
		TableInfo        TableInfo
		Create           Create
		Drop             Drop
		SqlExecStopTime  time.Duration
		Input            StartPartConfigInputP
		Optimizer        WriteOptimizer
		OptimizerOrderBy OptimizerOrderBy
		Alter            Alter
	}
	IndexMetaInfo struct {
		Type        string
		Name        string
		IndexColumn []IndexColumn
	}
	IndexMetaGather struct {
		Src        IndexMetaInfo
		Dst        IndexMetaInfo
		PartColumn []string //目标端的partition列集合
	}
	ColumnMetaGather struct {
		Logo              string /* 列 数据标识，一般是列名*/
		Src, Dst          TableMeta
		ColumnFilter      []string
		Attributes        *MetaAttributesAb
		TableMeta         GetTableMeta
		Comment           map[string]string //表的comment备注信息
		ColumnMeta        map[string] /*source/dest*/ []TableMeta
		IndexMeta         map[string] /*source/dest*/ IndexMerge
		PartitionMeta     map[string] /*source/dest*/ []TablePartitionConfig
		AlterStatus       bool //判断是否为create,true为alter，false为create
		MetaAbnormal      *MetaModifyAttributesAb
		IndexAbnormal     *IndexModifyAttributesAb
		PartitionAbnormal *PartitionModifyAttributesAb
		TableColumn       []map[string]string
	}
	PartitionMetaGather struct {
		Name             string
		SrcMeta, DstMeta map[string]SubPartitionMeta /*源目标端表结构校验用到的*/
	}
	TablesMetaInfoInput struct {
		LogSeq             int64
		DB                 map[string]*sql.DB
		TableInfo          TableInfo
		IndexMetaGather    IndexMetaGather     /* 索引校验时使用的原始数据 */
		ColumnMetaGather   ColumnMetaGather    /* 表结构校验时使用的原始数据 */
		PartitionMetaGarth PartitionMetaGather /*分区表校验时使用的原始数据*/
		IndexColumn        []IndexColumn
		Input              StartPartConfigInputP
	}
)

type (
	Return struct {
		Sql          string
		TimeOut      int64
		InsideResult any
		Result       any
	}
)
